<template>
	<view class="container">
		<u-modal v-model="show" :content="cont"></u-modal>
		<u-form :model="announce" :rules="rules" ref="uForm" :border-bottom="false">
			<u-form-item label="标题" label-width="150rpx" label-position="top" prop="title">
				<u-input v-model="announce.title" :border="border" />
			</u-form-item>
			<u-form-item label="内容" label-width="150rpx" label-position="top" prop="content">
				<u-input v-model="announce.content" :type="type" :border="border" :height="height" :auto-height="autoHeight" />
			</u-form-item>
		</u-form>
		<view class="but">
			<u-button type="primary" shape="circle" size="medium" :plain="true" @click="submit">提交</u-button>
		</view>
	</view>
</template>

<script>
	import api from '../../utils/api.js'
	export default {
		data() {
			return {
				show: false,
				cont: '发布成功',
				announce: {
					title: '',
					content: '',
					branchId:'',
					type:this.globalConst.INDEX_NOTIFICATION_TYPE
				},
				value: '',
				type: 'textarea',
				border: true,
				height: 300,
				autoHeight: true,
				rules: {
					title: [{
						required: true,
						message: '标题不能为空哦',
						trigger: 'change'
					}, ],
					content: [{
						required: true,
						message: '说一些内容吧(不能少于十个字)',
						min: 10,
						max: 500
					}]
				},
			};
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
			let user = uni.getStorageSync("user")
			this.announce.branchId = user.branchId
		},
		methods: {
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						api.postAnnounce(this.announce).then((res) => {
							if (res.code == 0) {
								this.cont = '发布成功';
								this.show = true;
								this.announce = {}
							}
						}).catch(() => {
							this.cont = '发布失败，请稍后再试'
							this.show = true
						})
					}
				})
			}
		}
	};
</script>



<style>
	.but {
		position: absolute;
		display: block;
		width: 50%;
		margin-top: 40%;
		margin-left: 30%;
	}

	.container {
		padding: 50rpx 50rpx 30rpx 30rpx;

	}
</style>
